package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService_f;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.management.MemoryUsage;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fr
 * @date 2020-03-07 11:55
 */

@RestController
@RequestMapping("/menu_f")
public class MenuController_f {

    @Reference
    MenuService_f menuService_f;

    //分页查询
    @RequestMapping("/findPage_f")
    public Result findPage_f(@RequestBody QueryPageBean pageBean){
        try {
            PageResult pageResult = menuService_f.findMenu(pageBean);
            return new Result(true,MessageConstant.GET_MENU_SUCCESS,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MENU_FAIL);
        }
    }
    //根据id查询子节点
    @RequestMapping("/findChild_f")
    public Result findChild_f(Integer id){
        try {
            List<Menu> childMenus = menuService_f.findChild(id);
            return new Result(true,MessageConstant.GET_MENU_SUCCESS,childMenus);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MENU_FAIL);
        }
    }


    //添加自关联id数据回显
    @RequestMapping("/findIds_f")
    public Result findIds_f(){
        try {
            List<Menu> menus = menuService_f.findAll();
            return new Result(true,MessageConstant.GET_MENU_SUCCESS,menus);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MENU_FAIL);
        }
    }
    //添加菜单项
    @RequestMapping("/addMenu_f")
    public Result addMenu_f(@RequestBody Menu menu){
        if (menu.getName()==null||menu.getLinkUrl()==null||menu.getPriority()==null||
        menu.getParentMenuId()==null|| menu.getPath()==null||menu.getLevel()==null){
            return new Result(false,MessageConstant.ADD_MENU_FAIL);
        }
        try {
            menuService_f.addMenu(menu);
            return new Result(true,MessageConstant.ADD_MEUN_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_MENU_FAIL);
        }
    }

    //编辑菜单数据回显
    @RequestMapping("/findById_f")
    public Result findById_f(Integer id){
        try {
            Menu menu = menuService_f.findById(id);
            List<Menu> menus = menuService_f.findAll();
            Map<String,Object> map=new HashMap<>();
            map.put("menu",menu);
            map.put("menus",menus);
            return new Result(true,MessageConstant.GET_MENU_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MENU_FAIL);
        }
    }

    //编辑菜单项
    @RequestMapping("/editMenu_f")
    public Result editMenu_f(@RequestBody Menu menu){
        if (menu.getName()==null||menu.getLinkUrl()==null||menu.getPriority()==null||
                menu.getParentMenuId()==null|| menu.getPath()==null||menu.getLevel()==null){
            return new Result(false,MessageConstant.EDIT_MENU_FAIL);
        }
        try {
            menuService_f.editMenu(menu);
            return new Result(true,MessageConstant.EDIT_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_MENU_FAIL);
        }
    }

    //删除菜单项
    @RequestMapping("/delMenu_f")
    public Result delMenu_f(Integer id){
        try {
            menuService_f.delMenu(id);
            return new Result(true,MessageConstant.DELETE_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_MENU_FAIL);
        }
    }
}
